package com.lankystar.todolist.page.task;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lankystar.common.Page;
import com.lankystar.common.Result;
import com.lankystar.common.util.StringHandler;
import com.lankystar.todolist.model.Account;
import com.lankystar.todolist.model.Task;
import com.lankystar.todolist.model.Taskrecord;
import com.lankystar.todolist.service.IAccountService;
import com.lankystar.todolist.service.ITaskService;

@Controller
@RequestMapping("/task")
public class TaskAction {
	@Autowired
	private ITaskService taskService;

	@RequestMapping("/toTask")
	public ModelAndView toTask() {
		return new ModelAndView("task/layout/task.html");
	}

	/**
	 * 指派任务页面
	 * 
	 * @return
	 */
	@RequestMapping("/assignTask")
	public ModelAndView assignTask(HttpServletRequest req) {
		List<Map<String, String>> taskpage = taskService.getAssignTasks();
		req.setAttribute("assigntask", taskpage);
		return new ModelAndView("task/layout/assigntask.html");
	}

	/**
	 * 查看任务
	 */

	@RequestMapping("/showTask")
	public ModelAndView checkTask(HttpServletRequest req) {
		List<Map<String,String>> tasklist= taskService.lookTask();
		req.setAttribute("tasklist", tasklist);
		return new ModelAndView("task/layout/showtask.html");
	}
	
	@RequestMapping(value = "/creatTask", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> createTask(HttpServletRequest req, String taskname, String tasktime, String stime,
			String etime, String ttask, String tmemo) {
		Result<String> result = new Result<>();
		Task task = new Task();
		try {
			System.err.println(taskname + tasktime + stime + etime + ttask + tmemo);
			if (!StringHandler.isEmpty(taskname) && !StringHandler.isEmpty(tasktime) && !StringHandler.isEmpty(stime)
					&& !StringHandler.isEmpty(etime)) {
				task.setName(taskname);
				task.setEstimatedtime(Integer.valueOf(tasktime));
				task.setStarttime(stime);
				task.setEndtime(etime);
				String ddate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
				task.setCreatetime(ddate);
				task.setMemo(tmemo);
				task.setUserid(((Account) req.getSession().getAttribute("user")).getId());
				task.setStatus(Integer.valueOf(ttask));
				taskService.insert(task);
				result.setCode(Result.OK);
				result.setMsg("创建成功！");
				return result;
			} else {
				result.setCode(Result.FAIL);
				result.setMsg("数据不完整，请重新输入");
				return result;
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			result.setCode(Result.FAIL);
			result.setMsg("服务器异常");
			return result;

		}
	}

	/**
	 * 指派任务操作弹窗
	 * 
	 * @return
	 */
	@RequestMapping("/assign")
	@ResponseBody
	public ModelAndView assignTak(HttpServletRequest request, String taskId) {
		List<Map<String, String>> list = taskService.getTeam();
		request.setAttribute("tlist", list);
		request.setAttribute("taskId", taskId);
		return new ModelAndView("task/layout/showTeam.html");
	}

	/**
	 * 指派任务操作
	 * 
	 * @return
	 */
	@RequestMapping("/operTask")
	@ResponseBody
	public Result<String> operTask(String uId, String taskId) {
		Result<String> result = new Result<String>();
		try {
			if (!StringHandler.isEmpty(uId) && !StringHandler.isEmpty(taskId)) {
				Taskrecord task = new Taskrecord();
				task.setUserid(Integer.valueOf(uId));
				task.setTaskid(Integer.valueOf(taskId));
				String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
				task.setCreatetime(date);
				boolean flag = taskService.insert(task);
				if (flag) {
					result.setMsg("指派成功");
					result.setCode(Result.OK);
					return result;
				} else {
					result.setMsg("指派失败");
					result.setCode(Result.FAIL);
					return result;
				}
			} else {
				result.setMsg("数据异常");
				result.setCode(Result.FAIL);
				return result;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setMsg("服务器异常");
			result.setCode(Result.FAIL);
			return result;
		}
	}
	
	/**
     * 关联查询任务记录表
     * @return
     */
	@RequestMapping("/getTaskrecord")
	public ModelAndView getTaskrecord(HttpServletRequest req){
		List<Map<String,String>> taskrecords=taskService.getTaskrecord();
		req.setAttribute("taskrecords", taskrecords);
		return new ModelAndView("/task/layout/taskrecord.html");
	}
	
	/**
	 * 修改进度
	 * @param trId
	 * @param opeate
	 * @return
	 */
	@RequestMapping(value="/toupdateOperate")
	public ModelAndView toupdateOperate(HttpServletRequest req,String tname,String operate,Integer trId){
		req.setAttribute("tname", tname);
		req.setAttribute("trId", trId);
		req.setAttribute("operate", operate);
		return new ModelAndView("/task/layout/updateOperate.html");
	}
	
	/**
	 * 更新进度
	 * @param trId
	 * @param operate
	 * @return
	 */
	@RequestMapping(value="/updateOperate",method=RequestMethod.POST)
	@ResponseBody
	public Result<String> updateOperate(Integer trId,String operate){
		Result<String> result = new Result<>();
		if(trId==null||operate==null){
			result.setCode(Result.FAIL);
			result.setMsg("数据为获取到！！");
			return result;
		}else{
			Taskrecord taskrecord = taskService.selectTaskrecordById(trId);
			taskrecord.setOperate(operate);
			taskService.updateTaskrecord(taskrecord);
			result.setCode(Result.OK);
			return result;
		}
	}
}